//
//  NetworkTool+Status.swift
//  微博Demo
//
//  Created by 花化华 on 2016/11/27.
//  Copyright © 2016年 itcast. All rights reserved.
//

import Foundation
import UIKit

extension WBNetworkTools {
    /// 请求微博数据
    ///
    /// - parameter since_id:     返回比since_id大的微博 即比since_id时间晚的微博
    /// - parameter max_id:       返回比max_id小的微博
    /// - parameter access_token: token
    /// - parameter callBack:     完成回调
    func reqeustStatus(since_id: String, max_id: String, access_token: String, callBack: @escaping (Any?)->()) {
        
        let urlStr = "https://api.weibo.com/2/statuses/home_timeline.json"
        let method = "GET"
        let parameters = ["since_id": since_id,
                          "max_id": max_id,
                          "access_token": access_token]
        
        // 调用AFN 发起网络请求
        WBNetworkTools.sharedManager.request(url: urlStr, method: method, paramerters: parameters, callBack: {
            
            (responseObject) in
            callBack(responseObject)
            
        })
        
    }

    /// 发布微博(纯文字, 或者是有图片)
    ///
    /// - parameter status:       微博的文字
    /// - parameter access_token: token
    /// - parameter image:        需要上传的图片
    /// - parameter callBack:     完成回调
    func updateStatus(status: String, access_token: String, image: UIImage? = nil, callBack: @escaping (Any?)->()) {
        
        let method = "POST"
        let parameters = ["status": status,
                          "access_token": access_token]
        
        //如果有图片, 调用upload方法
        if let image = image {
            
            let urlStr = "https://upload.api.weibo.com/2/statuses/upload.json"
            let imageData = UIImagePNGRepresentation(image)!
            
            // 调用AFN 发起网络请求
            WBNetworkTools.sharedManager.upload(url: urlStr, method: method, paramerters: parameters, data: imageData, dataKey: "pic", fileName: "filename", callBack: { (response) in
                callBack(response)
            })
            
        }
        //如果没有图片, 调用requet方法
        else {
            let urlStr = "https://api.weibo.com/2/statuses/update.json"
            
            // 调用AFN 发起网络请求
            WBNetworkTools.sharedManager.request(url: urlStr, method: method, paramerters: parameters, callBack: {
                (responseObject) in
                callBack(responseObject)
            })
        }
        
    }

}
